home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C ++ / Applications / Pict2Ascii 1.03 / Src / GenText.cp < prev    next >
Encoding:
Text File  |  1997-05-22  |  1.5 KB  |  78 lines  |  [TEXT/CWIE]

  1. // =================================================================================
  2. //    GenText.cp                                        ©1997 BB's Team inc. All rights reserved
  3. // =================================================================================
  4. #include "PL_Utils.h"
  5. #include "GenText.h"
  6.  
  7.  
  8. // ctor
  9. GenText::GenText ()
  10.     : mText (nil)
  11.     , mAutoContrast (true)
  12. {}
  13.  
  14.  
  15. // dtor
  16. GenText::~GenText ()
  17. {
  18.     MakeRoom();
  19. }
  20.  
  21.  
  22. // Make room.
  23. void GenText::MakeRoom (void)
  24. {
  25.     if ( mText != nil ) {
  26.         DisposeHandle (mText);
  27.         mText=nil;
  28.     }
  29. }
  30.  
  31.  
  32. // The contrast checkbox has changed
  33. void GenText::SetContrast (Boolean inContrast)
  34. {
  35.     MakeRoom ();
  36.     mAutoContrast = inContrast;
  37. }
  38.  
  39.  
  40. // Generates the text
  41. void GenText::Update (FontLight& font, ImageLight& image)
  42. {
  43.     // ought to be already done at that point
  44.     MakeRoom ();
  45.  
  46.     // Allocate the Handle for the resulting text
  47.     PL_Utils::ForceNewHandle (mText, image.GetTextSize());
  48.     if (mText == nil)
  49.         return;
  50.  
  51.     // Prepare synchronization between the image and the font's lighness
  52.     float fMin, fMax, lightMin, lightMax;
  53.     font.GetMinMax (fMin, fMax);
  54.  
  55.     if (mAutoContrast)
  56.         image.GetMinMax (lightMin, lightMax);
  57.     else {
  58.         lightMin = 0.;
  59.         lightMax = 1.;
  60.     }
  61.     
  62.     // Compute each image character equivalent
  63.     Int32 k = 0;
  64.     float u;
  65.     float scale = (fMax-fMin)/(lightMax-lightMin);
  66.  
  67.     for (Int16 i=0 ; i<image.GetHeightN() ; i++) {
  68.         for (Int16 j=0 ; j<image.GetWidthN() ; j++) {
  69.             u = fMin + scale * (image[k]-lightMin);
  70.             (*mText)[k] = font.ComputeChar (u);
  71.             k++;
  72.         }
  73.         (*mText)[k++]='\r';
  74.     }
  75.  
  76.     return;
  77. }
  78.